package com.example.chatpdf;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;



import java.io.File;

public class test {

    public static final String X_API_KEY = "sec_BOu5SBqQDl4zlpcDL99RQigvSlqMP5tQ";

    public static final String UPLOAD_URL = "https://api.chatpdf.com/v1/sources/add-url";

    public static final String CHAT_URL = "https://api.chatpdf.com/v1/chats/message";

    public static UploadRes uploadUrl(String url) {
        HttpRequest post = HttpUtil.createPost(UPLOAD_URL);
        UploadParam param = new UploadParam();
        param.setUrl(url);
        post.contentType("application/json");
        post.header("x-api-key", X_API_KEY);
        post.body(JSONUtil.toJsonStr(param));
        System.out.println(post);
        HttpResponse response = post.execute();
        return JSONUtil.toBean(response.body(), UploadRes.class);
    }

    public static ChatRes chat(ChatParam param) {
        HttpRequest post = HttpUtil.createPost(CHAT_URL);
        post.contentType("application/json");
        post.header("x-api-key", X_API_KEY);
        post.body(JSONUtil.toJsonStr(param));
        HttpResponse response = post.execute();
        return JSONUtil.toBean(response.body(), ChatRes.class);
    }

    public static void main(String[] args) {
//        UploadRes uploadRes = uploadUrl("https://test.app.yingyeeai.com/oss/yye/20230814/89YLyj.pdf");
//        String sourceId = uploadRes.getSourceId();
//        System.out.println(sourceId);


        String sourceId = "src_3ZExtRpIOsfxhnliAwEJa";
        Message message = new Message();
        message.setRole("user");
        message.setContent("这份文件讲述了什么内容，请用中文回复");
        Message[] messages = {message};
        ChatParam param = new ChatParam();
        param.setMessages(messages);
        param.setSourceId(sourceId);
        ChatRes res = chat(param);
        System.out.println(res.getContent());

    }

}
